package com.zrrd.learn.framework.mybatis;
//mybatis io
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 用来完成学生表信息显示功能
 *
 * @author wrn
 * @version 1.0
 * @date 2023/8/30 10:28
 */

public class Demo01 {

    public List<Map<String,Object>> showAll(){
        String resource = "mybatisconfiguration.xml";
        InputStream inputStream = null ;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            return session.selectList("first.showall");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public List<Map<String,Object>> showWithParameter(String name){
        //总配置文件地址
        String resource = "mybatisconfiguration.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            return session.selectList("first.showWithparameter",name);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public List<Map<String,Object>> insert(){
        //总配置文件地址
        String resource = "mybatisconfiguration.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            return session.selectList("first.insert");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {

        LinkedList list = new LinkedList();
        list.add("A”");
        list.add(2, "B");
        String s = (String) list.get(1);
        System.out.println(s);




//        Demo01 demo01 = new Demo01();
//        demo01.showAll().forEach((y) -> {
//            y.forEach((k, v) -> {
//                System.out.print("key==>" + k + "\t");
//                System.out.print("value==>" + v + "\t");
//            });
//            System.out.println();
//        });

//        demo01.showWithParameter("王").forEach((y)->{
//            y.forEach((k,v)->{
//                System.out.print("key==>"+k+"\t");
//                System.out.print("value==>"+v+"\t");
//            });
//            System.out.println();
//        });

    }
}
